package com.xgzit.xgzmall.order.listener;

import com.rabbitmq.client.Channel;
import com.xgzit.common.dto.mq.SeckillOrderDto;
import com.xgzit.xgzmall.order.entity.OrderEntity;
import com.xgzit.xgzmall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author xgz
 * @Date 2021-07-16 21:05
 */
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Service
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(SeckillOrderDto orderDto, Channel channel, Message message) throws IOException {
        log.info("收到的秒杀订单信息是：{}", orderDto);
        try {
            orderService.createSeckillOrder(orderDto);
            // 手动调用支付宝收单

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            log.error("关闭订单异常！",e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
